<template>
  <el-form
    :model="form"
    label-width="180px"
    v-loading="isLoading"
  >
    <el-form-item label="允许修改用户名">
      <el-switch v-model="form.isUserNameUpdateEnabled" />
    </el-form-item>
    <el-form-item label="允许修改邮箱">
      <el-switch v-model="form.isEmailUpdateEnabled" />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="submit"
      >
        保存
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getUserRule, setUserRule } from '@/http/rule'
import { ElMessage } from 'element-plus'

const form = ref({
  isUserNameUpdateEnabled: true,
  isEmailUpdateEnabled: true
})
const isLoading = ref(false)

onMounted(() => {
  getData()
})

const getData = async () => {
  isLoading.value = true
  const res = await getUserRule()
  form.value = res
  isLoading.value = false
}

const submit = async () => {
  isLoading.value = true
  await setUserRule(form.value)
  isLoading.value = false
  ElMessage.success('保存成功')
}
</script>

<style scoped lang="scss">

</style>
